package com.jews.securityleam.starter.base;

import com.jews.securityleam.starter.util.HandleHttpErrorUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @author JewsChen
 * @title: JwtAuthenticationEntryPoint
 * @description: 用于处理凭证错误或无对应权限访问的入口
 * @date 2023/6/19 17:48
 */
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        try {
            HandleHttpErrorUtil.handleHttpError(request, response, authException);
        } catch (Exception var5) {
            response.sendError(HttpStatus.UNAUTHORIZED.value(), "Unauthorized");
        }
    }
}
